/**
 * Created by IntelliJ IDEA.
 * User: ljy
 * Date: 2021/6/18
 * Time: 15:04
 */
package com.cowcode.controller;

import com.cowcode.biz.UserBiz;
import com.cowcode.entity.User;
import com.cowcode.util.SendEmail;
import com.cowcode.util.Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("signup/")
public class SignupController {
    @Resource
    private UserBiz userBiz;

    @RequestMapping(value = "checkacc",method = RequestMethod.POST)
    @ResponseBody
    public int checkacc(String uacc){
        return uacc.equals(userBiz.checkacc(uacc))?0:1;
    }
    @RequestMapping(value = "checkphone",method = RequestMethod.POST)
    @ResponseBody
    public int checkphone(String uphone){
        return uphone.equals(userBiz.checkphone(uphone))?0:1;
    }
    @RequestMapping(value = "checkemail",method = RequestMethod.POST)
    @ResponseBody
    public int checkemail(String uemail){
        return uemail.equals(userBiz.checkemail(uemail))?0:1;
    }

    @RequestMapping(value = "phonecode",method = RequestMethod.POST)
    @ResponseBody
    public int phonecode(String uphone, HttpSession session) throws IOException {//发送手机短信的ajax方法
        // 779530904@qq.com: appid=96917271&appsecret=qf5oW00s
        // 958126601@qq.com: appid=56232161&appsecret=g1Hh7esa
        String code = Util.codeNum();
        session.setAttribute("code",code);
        String url = "https://yiketianqi.com/api/sms?appid=16268995&appsecret=dNlJS1Xk&code="+code+"&mobile="+uphone+"&template_id=103";
        System.out.println(Util.crawler(url));
        return 0;
    }
    @RequestMapping(value = "emailcode",method = RequestMethod.POST)
    @ResponseBody
    public int emailcode(String uemail,HttpSession session){
        String code = Util.CodeZiMu();
        session.setAttribute("code",code);
        return SendEmail.EmailCode(uemail,code)?0:1;
    }


    @RequestMapping(value = "checkyzm",method = RequestMethod.POST)
    @ResponseBody
    public int checkyzm(String yzm,HttpSession session){
        String code = (String) session.getAttribute("code");
        return yzm.equals(code)?0:1;
    }

    @RequestMapping("signup")
    public String signup(Model model, User user,HttpSession session){
        System.out.println(user);
        if (userBiz.signup(user)) {
            model.addAttribute("msg","注册成功");
            user.setUid(userBiz.queryId(user.getUacc()));
            System.out.println("user = " + user);
            session.setAttribute("user",user);
            model.addAttribute("jump","toindex");
            return "redirect:/jump/toindex";
        } else {
            model.addAttribute("msg","注册失败");
            model.addAttribute("jump","tosignup");
            return "redirect:/jump/signup";
        }
    }
}
